﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.CustomerSVC;
using SMS.Entities;
using SMS.Controls.ProductSVC;

namespace SMS.Model
{
    public class ProductMasterModel
    {
        ServiceProxy<ProductServiceClient> proxy = null;

        public event Action<int> OnSaveCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<Product>> OnSearchCompleted;
        public event Action<SMSException> OnServiceError;
        public ProductMasterModel()
        {
            proxy = new ServiceProxy<ProductServiceClient>("Service/ProductService.svc");
            proxy.Client.SaveProductCompleted += new EventHandler<SaveProductCompletedEventArgs>(Client_SaveProductCompleted);
            proxy.Client.SearchProductCompleted += new EventHandler<SearchProductCompletedEventArgs>(Client_SearchProductCompleted);
        }

        void Client_SearchProductCompleted(object sender, SearchProductCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSearchCompleted != null)
                    OnSearchCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);    
        }

        void Client_SaveProductCompleted(object sender, SaveProductCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveCompleted != null)
                    OnSaveCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void SaveProduct(Product product)
        {
            proxy.Client.SaveProductAsync(App.LoggedinUserName, product);
        }

        public void SearchProduct(string SearchText)
        {
            proxy.Client.SearchProductAsync(App.LoggedinUserName, SearchText);
        }
    }
}
